// 对 requst 请求进行统一封装 支持promise
// 如果需要进行同步 可在方法重新定义方法 封装 async await

var app = getApp();
var baseUrl = app.globalData.baseUrl
// var app = getApp();
// var baseUrl = app.globalData.baseUrl
// http://123.57.225.36:8099
// var baseUrl = 'https://glowworm.club:8100'
var baseWelinkUrl = 'https://open.welink.huaweicloud.com'

// 通用请求方法,其他见名知意
const requstCommon = function (url, params, method, data, token) {
    if (params == null) {
        let promiseData = new Promise(function (resolve, reject) {
            wx.request({
                url: baseUrl + url,
                data: data,
                method: method,
                header: {
                    'content-type': 'application/json',
                    // 'Authorization': token
                    'Authorization': wx.getStorageSync("token")||'',
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        // console.log("网络联通")
                        resolve(res)
                    } else {
                        // console.log("网络statuscode!=200")
                        reject(res)
                    }
                },
                fail: function (err) {
                    console.log("err")
                    reject(err)
                    // if (err.code === 401) {}
                }
            })
        })
        return promiseData
    } else {
        let promiseData = new Promise(function (resolve, reject) {
            wx.request({
                url: baseUrl + url + params,
                data: data,
                method: method,
                header: {
                    'content-type': 'application/json',
                    // 'Authorization': token
                    'Authorization': wx.getStorageSync("token")||'',
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        resolve(res)
                    } else {
                        reject(res)
                    }
                },
                fail: function (err) {
                    reject(err)
                    if (err.code === 401) {}
                }
            })
        })
        return promiseData
    }

}


// welink登录接口调用
const requstWelink = function (url, params, method, data) {
    if (params == null) {
        let promiseData = new Promise(function (resolve, reject) {
            wx.request({
                url: baseWelinkUrl + url,
                data: data,
                method: method,
                header: {
                    'content-type': 'application/json',
                    'x-wlk-Authorization': wx.getStorageSync("welinkToken")||'ss',
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        // console.log("网络联通")
                        resolve(res)
                    } else {
                        // console.log("网络statuscode!=200")
                        reject(res)
                    }
                },
                fail: function (err) {
                    console.log("err")
                    reject(err)
                    // if (err.code === 401) {}
                }
            })
        })
        return promiseData
    } else {
        let promiseData = new Promise(function (resolve, reject) {
            wx.request({
                url: baseWelinkUrl + url + params,
                data: data,
                method: method,
                header: {
                    'content-type': 'application/json',
                    'x-wlk-Authorization': wx.getStorageSync("welinkToken")||'ss',
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        resolve(res)
                    } else {
                        reject(res)
                    }
                },
                fail: function (err) {
                    reject(err)
                    if (err.code === 401) { }
                }
            })
        })
        return promiseData
    }

}



const requstHaveToken = function (url, data, token, method) {
    let promiseData = new Promise(function (resolve, reject) {
        wx.request({
            url: baseUrl + url,
            data: data,
            method: method,
            header: {
                'content-type': 'application/json',
                'Authorization': token
            },
            success: function (res) {
                if (res.statusCode === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
                if (err.code === 401) {}
            }
        })
    })
    return promiseData
}
const getHaveToken = function (url, data, token) {
    let promiseData = new Promise(function (resolve, reject) {
        wx.request({
            url: baseUrl + url,
            data: data,
            method: 'GET',
            header: {
                'content-type': 'application/json',
                'Authorization': token
            },
            success: function (res) {
                if (res.statusCode === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
                if (err.code === 401) {}
            }
        })
    })
    return promiseData
}
const requst = function (url, data, method) {
    let promiseData = new Promise(function (resolve, reject) {
        wx.request({
            url: baseUrl + url,
            data: data,
            method: method,
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                if (res.statusCode === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
                if (err.code === 401) {}
            }
        })
    })
    return promiseData
}
const get = function (url) {
    let promiseData = new Promise(function (resolve, reject) {
        wx.request({
            // url: url,
            url: baseUrl + url,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                if (res.statusCode === 200) {
                    resolve(res)
                } else {
                    reject(res)
                }
            },
            fail: function (err) {
                reject(err)
                if (err.code === 401) {}
            }
        })
    })
    return promiseData
}
module.exports = {
    requstCommon,
    requstHaveToken,
    getHaveToken,
    requst,
    get,
    requstWelink
    // requstHaveToken,
    // getHaveToken,
    // requst,
    // get
}